package com.example.kotlinview

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.widget.TextView
import okhttp3.*
import org.jetbrains.anko.textColor
import java.io.IOException
import java.util.logging.Logger
import kotlin.concurrent.thread

class ktOkhttpActivity : AppCompatActivity() {

    var ktv: TextView? = null;
    val MSG_RESP_BAIDU = 0;
    var resp: Response?=null;
    val url="https:www.baidu.com"
    val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                MSG_RESP_BAIDU-> {
                    ktv?.text=msg.obj.toString();
                }
                else -> {
                }
            }
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kt_okhttp)
        ktv=findViewById<TextView>(R.id.ktv);
        ktv?.text="hello Kotlin"
        ktv?.textColor= Color.BLACK

        execute()
//        enqueue()

    }


    /**
     * Execute
     * 同步
     */
    fun execute(){
        thread {
            var okHttpClient = OkHttpClient()
            var request = Request.Builder()
                .url(url)
                .build()
            val res=okHttpClient.newCall(request).execute()
            var data =res?.body?.string()
//
//            runOnUiThread(){
//                if (data!=null) {
//                    ktv?.text=data;
//                }
//            }
            var message = Message()
            message.obj=data;
            message.what=MSG_RESP_BAIDU;
            handler.sendMessage(message)
        }
    }

    /**
     * Enqueue
     * 异步
     */
    fun enqueue(){
        thread{
            var okHttpClient = OkHttpClient()
            var request = Request.Builder()
                .url(url)
                .build()
            okHttpClient.newCall(request)
                .enqueue(object : Callback {//对象表达式
                override fun onFailure(call: Call, e: IOException) {
                    Logger.getLogger(e.toString())
                }

                    override fun onResponse(call: Call, response: Response) {
                        resp=response;
                        var data =resp?.body?.string()

                        var message = Message()
                        message.obj=data;
                        message.what=MSG_RESP_BAIDU;
                        handler.sendMessage(message)
                    }
                })

//            对象表达式
//            object 关键字不仅仅能用来声明单例模式的对象，还能用来声明匿名对象，
//            与 Java 匿名内部类只能扩展一个类或实现一个接口不同 ，
//            Kotlin 的匿名对象可以实现多个接口或者不实现接口，
//            我们称之为对象表达式。
//
//
//
//            interface MyInterface {
//                fun doSomething()
//                fun doOtherthing()
//            }
//
//            val myInstant = object : MyInterface {
//                override fun doSomething() {
//                    ...
//                }
//
//                override fun doOtherthing() {
//                    ...
//                }
//            }

        }
    }
}